<template>
	<el-card class="box-card table">
		<template #header>
			<div class="card-header">
				<span>
					<el-breadcrumb separator="/">
						<el-breadcrumb-item :to="{ path: '/product/category/list' }">商品分类</el-breadcrumb-item>
						<el-breadcrumb-item>新增</el-breadcrumb-item>
					</el-breadcrumb>
				</span>
				<router-link :to="{ path: '/product/category/list' }">
					<el-button class="button" type="success" icon="el-icon-back" size="small">返回</el-button>
				</router-link>
			</div>
		</template>
		<div class="body">
			<el-form :model="form" ref="form" status-icon :rules="rules" label-width="80px" size="small">
				<el-row>
					<el-form-item label="名称" prop="name">
						<el-input v-model="form.name"></el-input>
					</el-form-item>
				</el-row>

				<el-row>
					<el-button type="primary" @click="submitForm('form')">立即创建</el-button>
					<el-button @click="resetForm('form')">重置</el-button>
				</el-row>

			</el-form>
		</div>
	</el-card>
</template>

<script>
	import {
		productCategoryAdd
	} from "@/api/index.js";
	import {
		ElMessage
	} from "element-plus";
	export default {
		name: "ProductCategoryAdd",
		data() {
			return {
				form: {
					name: null,
				},
				rules: {
					name: [{
						required: true,
						message: "请输入名称",
						trigger: "blur"
					}],
				},
			};
		},
		mounted() {},
		methods: {
			async submitForm(form) {
				//自动验证
				this.$refs[form].validate((valid) => {
					if (!valid) {
						return false;
					}
				});
				await productCategoryAdd(this.form)
					.then((res) => {
						console.log(res);
						if (res.code == 1) {
							this.$message.success("新增成功");
							this.$router.push('/product/category/list');
						} else {
							this.$message.error("新增失败");
						}
					})
					.catch((error) => {});
			},
			resetForm(form) {
				this.$refs[form].resetFields();
			},
		},
	};
</script>